package com.cliente;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.google.gson.Gson;

import domain.AdministradorVO;

import android.util.Log;

public class Controller {
	
	private static final String IP = "192.168.1.103";
	private static final String URL = "http://"+IP+"/encryp/service/application/servicio_encryp/servicio_encrypSOA.php";
	private static final String NAMESPACE = "http://"+IP+"/encryp/service/application/servicio_encryp/servicio_encrypSOA.php";

	
	public String logear(String user, String pass) {
		
		String METHOD_NAME = "generarLogin";
		String SOAP_ACTION = NAMESPACE +"#"+ METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		// transformando a JSON
		domain.AdministradorVO admin = new AdministradorVO();
		admin.setUsuario(user);
		admin.setContrasena(pass);
		final Gson gson = new Gson();
		String send = gson.toJson(admin);

		// agregando parametro
		request.addProperty("AdministradorVOJson", send.toString());

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			String response = (String) envelope.getResponse();
			Log.i("Webservice Output", response.toString());

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
			Log.i(METHOD_NAME, "" + e);
			return "error de conexion";
		}
	}
	
	public String agregar(String texto, String token,String funcion) {
		
		SoapObject request;
		
		String METHOD_NAME = "encriptar";
		String SOAP_ACTION = NAMESPACE +"#"+ METHOD_NAME;
		
		if (funcion.equals("encr")){
			request = new SoapObject(NAMESPACE, METHOD_NAME);
		}else{
			request = new SoapObject(NAMESPACE, "desencriptar");
		}

		// agregando parametro
		request.addProperty("texto", texto);
		request.addProperty("token", token);

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			if (funcion.equals("encr")){
				androidHttpTransport.call(SOAP_ACTION, envelope);
			}else{
				androidHttpTransport.call(NAMESPACE +"#"+ "desencriptar", envelope);
			}
			String response = (String) envelope.getResponse();
			Log.i("Webservice Output", response.toString());

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}
	
	public String getLista(String token) {
		
		String METHOD_NAME = "mostrarHistorico";
		String SOAP_ACTION = NAMESPACE +"#"+ METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		request.addProperty("token", token);

		// configurando envio
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			String response = (String) envelope.getResponse();
			Log.i("Webservice Output", response.toString());

			return response.toString();

		} catch (Exception e) {
			e.printStackTrace();
			Log.i(METHOD_NAME, "" + e);
			return "error de conexion";
		}
	}
}
